﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using Abhiruchi_Phase1.BAL_DAL.ORG_DAL;

namespace Abhiruchi_Phase1
{
    /// <summary>0
    /// Interaction logic for UC_ORG_Location.xaml
    /// </summary>
    public partial class UC_ORG_Location : UserControl
    {
        static string id = "0";
        public UC_ORG_Location()
        {
            InitializeComponent();
            lblMode.Content = "New";
            // To Bind Location Master Data Into That Telerik Grid Controle
            BindLocationGridData();

            // To Load Location Incharge Name Insted of Employee Id 
            LoadLocInchargeName();

            // To Increment The Max Location Id Count
            LocationIdCount();
        }
        public void BindLocationGridData()
        {
            DataTable dt = new DataTable();
            dt = LocationMasterDAL.GetORGLoactionInchargeName();
            DataRow dtrow;
            for (int i = 0; i < 15; i++)
            {
                dtrow = dt.NewRow();
                dtrow["LocationId"] = DBNull.Value;
                dtrow["LocationName"] = "";
                dtrow["Address"] = "";
                dtrow["Location Incharge"] = "";
                dtrow["ContactNumber"] = "";
                dtrow["Status"] = null;
                dt.Rows.Add(dtrow);
            }
            
            radGridLocData.ItemsSource = dt;

        }

        public void LoadLocInchargeName()
        {
            DataTable dtEName = new DataTable();
            dtEName = LocationMasterDAL.GetORGLoactionENames();
            cmbLocInchrge.DataContext = dtEName.DefaultView;
            cmbLocInchrge.DisplayMemberPath = dtEName.Columns["EName"].ToString();
            cmbLocInchrge.SelectedValuePath = dtEName.Columns["EId"].ToString();
        }

        public void LocationIdCount()
        {
            DataTable mydt = new DataTable();
            mydt = LocationMasterDAL.GetORGLoactionId();
            if (mydt.Rows.Count > 0)
            {
                if (mydt.Rows[0][0].ToString() == "0")
                {
                    lblLocId.Text = "1";
                    lblLocId.Text=mydt.Rows[0][0].ToString();
                }
                else
                {
                    lblLocId.Text = (Int32.Parse(mydt.Rows[0][0].ToString()) + 1).ToString();
                }
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (Validations())
            {
                MessageBoxResult msgRes = MessageBox.Show("Do you want to Save this Location Master Information", "Confirmation", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

                if (msgRes == MessageBoxResult.Yes)
                {
                    int chk;
                    if (chkIsActive.IsChecked == true)
                    {
                        chk = 1;
                    }
                    else
                    {
                        chk = 0;
                    }
                    if (id == "0")

                    {
                        LocationMasterDAL.GetORGLocationMasterInsert(txtLocName.Text, txtAddress.Text, cmbLocInchrge.SelectedValue.ToString(), txtContactNo.Text, chk);
                        MessageBox.Show("Information Inserted Sucessfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                        ClearFields();
                        LocationIdCount();
                    }
                    else
                    {
                        LocationMasterDAL.GetLocationMasterUpdate(Int32.Parse(lblLocId.Text), txtLocName.Text, txtAddress.Text, cmbLocInchrge.SelectedValue.ToString(), txtContactNo.Text, chk);
                        MessageBox.Show("Information Updated Sucessfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                        ClearFields();
                        LocationIdCount();
                        id = "0";
                    } 
                    BindLocationGridData();
                }
            }
        }

        private void radGrdData_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            lblMode.Content = "Edit";     
    
            if (((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value== null)
            {
                btnClear_Click(this, e);
                return;                
            }
            id = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            lblLocId.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            txtLocName.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            txtAddress.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            cmbLocInchrge.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString();
            txtContactNo.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[4])).Value.ToString();
            if (((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[5])).Value.ToString() == "No")
            {
                chkIsActive.IsChecked = false;
            }
            else
            {
                chkIsActive.IsChecked = true;
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            lblMode.Content = "New";
            ClearFields();
            LocationIdCount();
        }

        private void ClearFields()
        {
            txtLocName.Text = "";
            txtAddress.Text = "";
            cmbLocInchrge.SelectedIndex = -1;
            txtContactNo.Text = "";
            chkIsActive.IsChecked = true;
            radGridLocData.SelectedItem = "";
            id = "0";
        }

        #region Validation
        private bool Validations()
        {
            lblLocName.Foreground = System.Windows.Media.Brushes.Black;
            lblAddress.Foreground = System.Windows.Media.Brushes.Black;
            lblLocInchrge.Foreground = System.Windows.Media.Brushes.Black;
            lblContactNo.Foreground = System.Windows.Media.Brushes.Black;
            if (txtLocName.Text == "")
            {
                MessageBox.Show("Please Enter Location Name", "Location Name", MessageBoxButton.OK, MessageBoxImage.Warning);
                lblLocName.Foreground = System.Windows.Media.Brushes.Red;
                txtLocName.Focus();                
                return false;
            }
            else
            {
                lblLocName.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (txtAddress.Text == "")
            {
                MessageBox.Show("Please Enter Address", "Address", MessageBoxButton.OK, MessageBoxImage.Warning);
                lblAddress.Foreground = System.Windows.Media.Brushes.Red;  
                txtAddress.Focus();                  
                return false;
            }
            else
            {
                lblAddress.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (cmbLocInchrge.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Location Incharge Name", "Location Incharge", MessageBoxButton.OK, MessageBoxImage.Warning);
                lblLocInchrge.Foreground = System.Windows.Media.Brushes.Red;
                cmbLocInchrge.Focus();                
                return false;
            }
            else
            {
                lblLocInchrge.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (txtContactNo.Text == "")
            {
                MessageBox.Show("Please Enter Contact Number", "Contact Number", MessageBoxButton.OK, MessageBoxImage.Warning);
                lblContactNo.Foreground = System.Windows.Media.Brushes.Red;
                txtContactNo.Focus();                
                return false;
            }
            else
            {
                lblContactNo.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (txtContactNo.Text != "")
            {
                lblContactNo.Foreground = System.Windows.Media.Brushes.Black;
                if (!Regex.Match(txtContactNo.Text, @"^[-+]?[0-9]*\.?[0-9]+$").Success)
                {
                    MessageBox.Show("Please Enter Valid Contact Number", "Proper Contact Number", MessageBoxButton.OK, MessageBoxImage.Warning);
                    lblContactNo.Foreground = System.Windows.Media.Brushes.Red;
                    txtContactNo.Focus();                    
                    return false;
                }
                else
                {
                    lblContactNo.Foreground = System.Windows.Media.Brushes.Black;
                }
            }            
            return true;
        }
        #endregion

        private void txtLocName_LostFocus(object sender, RoutedEventArgs e)
        {

            if (txtLocName.Text != "")
            {
                DataTable dt = new DataTable();
                dt = LocationMasterDAL.GetORGLoactionNameCompare(Int32.Parse(lblLocId.Text), txtLocName.Text);
                if (!(dt.Rows[0][0].ToString() == "0"))
                {
                    MessageBox.Show("Location " + txtLocName.Text + " Already Utilized, Please Give Different Location Name", "Duplicate Location Name", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtLocName.Text = "";
                    txtLocName.Focus();
                    lblLocName.Foreground = System.Windows.Media.Brushes.Red;
                }
                else
                {
                    lblLocName.Foreground = System.Windows.Media.Brushes.Black;
                }
            }
        }       
    }
}
